<?php
namespace App\Http\Controllers;

use App\Model\NewActivityOrder;
use Illuminate\Http\Request;
use DB;
use App\Http\Common;
use App\Model\Info;



class OrderController extends Controller {

	use Common;

    public $current = 'Order';

    public $info;
    public $adminUserIdArr = [1,117];


	public function __construct(Request $request)
    {
        $this->isAdmin = 0;
        $this->orderStatus = 1;

        $this->order = new NewActivityOrder();

        $this->middleware('nocacheview', ['only' => [
                                                'getIndex',
                                                'getCreate',
                                                'getUpdate',
                                            ]
            ]);
    }

	public function getIndex(Request $request)
	{
        $data = $request->all();
        $admin = session()->get('adminInfo');
        if(in_array($admin->user_id, $this->adminUserIdArr)){
            $this->isAdmin = 1;
        }
        $dataArr['status'] = $this->orderStatus;
        if(isset($data['status'])){
            $this->orderStatus = $data['status'];
            $dataArr['status'] = $this->orderStatus;
        }
//        echo '<Pre>';print_r($data);die();
        $list = $this->order->getList($data['id'], $this->isAdmin, $admin->user_id, $dataArr);
//        echo '<Pre>';print_r($list);die();

        $list->appends(['id' => $data['id'], 'status' => $dataArr['status']]);//url中增加活动ID
		return view('order.index', [
            'data' => $list,
            'current' => $this->current,
            'activityId' => $data['id'],
            'isAdmin' => $this->isAdmin,
            'orderStatus' => $this->orderStatus,
        ]);
	}
    
    public function getCreate()
    {   
        return view('info.create', ['current' => $this->current]);
    }

    public function postCreate(Request $request)
    {   
        $data = $request->all();
        
        $rs = $this->recordObj->create($data);

        return $this->successReturn('创建成功');
    }

    public function getView(Request $request)
    {
        $data = $request->all();
        $admin = session()->get('adminInfo');
        $orderData = $this->order->getInfoById($data['id'], $admin->user_id);
//        echo '<Pre>';print_r($orderData);die();

        return view('order.update', ['data' => $orderData, 'current' => $this->current]);
    }

    public function postUpdate(Request $request)
    {
        $data = $request->all();
       
        $id = $data['id'];

        unset($data['id']);

        $rs = $this->info->update($data, $id);

        return $this->successReturn('更新成功');
    }

    public function postDelete(Request $request)
    {
        $id = $request->input('id');

        $rs = $this->info->delete($id);

        return $this->successReturn('删除成功');
    }


    //接受机器人点赞数量
    public function postLike(Request $request)
    {
        $num = $request->input('num');

        $id = $request->input('id');

        if(!is_numeric($num)) {
            return $this->errorReturn('请输入数字');
        }

        if($num > $this->robot->count() || $num <= 0) {
            return $this->errorReturn('输入的数字超过机器人总数 或者过小');
        }

        $rs = $this->info->robotLike($num, $id);

        return $rs ? $this->successReturn('操作成功') : $this->errorReturn('操作失败');

    }



}